﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TrickleToCollection.cs" company="XamlNinja">
//   2011 Richard Griffin and Ollie Riches
// </copyright>
// <summary>
//   Trickles only unique values not already in the destination collection...
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace WP7Contrib.Collections
{
    using System;
    using System.Reactive.Concurrency;
    using System.Windows.Threading;

    public sealed class TrickleUniqueToCollection<T> : BaseTrickleToCollection<T>
    {
        public TrickleUniqueToCollection(Action addedAction, Action completedAction)
            : this (addedAction, () => {}, () => {}, () => {}, () => {}, completedAction)
        {
        }

        public TrickleUniqueToCollection(Action addedAction, Action startedAction, Action stoppedAction, Action suspendedAction, Action resumedAction, Action completedAction)
           : base (addedAction, startedAction, stoppedAction, suspendedAction, resumedAction, completedAction)
        {
            this.DispatcherTimer = new DispatcherTimer();
            this.DispatcherTimer.Interval = this.StartupDelay;
            this.DispatcherTimer.Tick += delegate
                                             {
                                                 if (this.DispatcherTimer.IsEnabled)
                                                 {
                                                     if (this.SourceQueue.Count != 0)
                                                     {
                                                         var item = this.SourceQueue.Dequeue();
                                                         if (!this.Destination.Contains(item))
                                                         {
                                                             this.Destination.Add(item);
                                                             Scheduler.CurrentThread.Schedule(this.AddedAction);
                                                         }

                                                         if (this.Count == 0)
                                                            this.DispatcherTimer.Interval = this.Delay;

                                                         this.Count++;
                                                     }
                                                     else
                                                     {
                                                         this.DispatcherTimer.Stop();
                                                         this.CompletedAction();
                                                     }
                                                 }
                                             };
        }
    }
}